<?php

namespace App\Http\Controllers\Admin\Badge;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;
use Illuminate\Support\Facades\Validator;

class BadgeController extends BaseController {

    /**
     * @api {post} /badge/manager 徽章创建
     * @apiVersion 0.1.0
     * @apiName 徽章管理-徽章创建
     * @apiGroup 徽章管理
     *
     * @apiParam {number} status 状态 1开启2隐藏 必传
     * @apiParam {number} type_id 类别id  必传
     * @apiParam {string} name                         名称 必传
     * @apiParam {number} sort                         排序 必传
     * @apiParam {string} picture                      图片 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'type_id' => 'required|numeric|exists:mysql.ln_badge_type,id',
                'picture' => 'required|string',
                'name' => 'required|string',
                'sort' => 'required|numeric|integer|min:1',
                'status' => 'required|numeric|integer|in:1,2',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $params = $request->input();

        $res = app('repo_badge');
        $params['add_time'] = time();

        $rows = $res->addBadge($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /badge/manager 徽章删除
     * @apiVersion 0.1.0
     * @apiName 徽章管理-徽章删除
     * @apiGroup 徽章管理
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request)
    {
        $v = app('validator')->make($request->input(),[
            'ids' => 'required|string',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_badge')->delBadge($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /badge/manager 徽章修改
     * @apiVersion 0.1.0
     * @apiName 徽章管理-徽章修改
     * @apiGroup 徽章管理
     *
     *
     * @apiParam {number} id
     * @apiParam {number} status 状态 1开启2隐藏 必传
     * @apiParam {number} type_id 类别id  必传
     * @apiParam {string} name                         名称 必传
     * @apiParam {number} sort                         排序 必传
     * @apiParam {string} picture                      图片 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(), [
            'id' => 'required|integer',
            'type_id' => 'required|numeric|exists:mysql.ln_badge_type,id',
            'picture' => 'required|string',
            'name' => 'required|string',
            'sort' => 'required|integer|min:1',
            'status' => 'required|numeric|integer|in:1,2',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        $res = app('repo_badge');

        $params = $request->input();
        $id = $params['id'];

        $rows = $res->saveBadge(["id" => $id], $params);
//        if (!$rows) {
//            throw new BasicException(10001, "更新失败！");
//        }

        return Response::sendData(true);
    }

    /**
     * @api {post} /badge/manager/give 赠送徽章
     * @apiVersion 0.1.0
     * @apiName 徽章管理-赠送徽章
     * @apiGroup 徽章管理
     *
     *
     * @apiParam {number} badge_id 徽章id 必传
     * @apiParam {number} user_code 用户编号  必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function give(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(), [
            'badge_id' => 'required|numeric|exists:mysql.ln_badge,id',
            'user_code' => 'required|numeric|exists:mysql.ln_user,user_no',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        $res = app('repo_badge');

        $params = $request->input();
        $params['type'] = 1;
        $params['add_time'] = time();

        $user = app("model_user")->where("user_no",$params['user_code'])->select(["user_id"])->first();
        unset($params['user_code']);
        $params['user_id'] = $user->user_id;

        $rows = $res->addBadgeUser($params);
        if (!$rows) {
            throw new BasicException(10001, "赠送失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /badge/manager 徽章列表
     * @apiVersion 0.1.0
     * @apiName 徽章管理-徽章列表
     * @apiGroup 徽章管理
     *
     * @apiParam {number} status 状态 1开启2隐藏 可选
     * @apiParam {number} type_id 类别id  可选
     * @apiParam {string} keyword 关键字 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "id(序号)": 12,
                "picture(图片)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5fa21c40ba932.png",
                "name(名称)": "中秋徽章",
                "sort(排序)": 1,
                "type_id(类别id)": 7,
                "add_time": 0,
                "status(状态)": 1,
                "badge_type": {
                    "id": 8,
                    "name(类别名称)": "学霸徽章"
                }
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(), [
                'status' => 'filled|numeric|in:1,2',
                'type_id' => 'filled|numeric|exists:mysql.ln_badge_type,id',
                'keyword' => 'filled|string',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_badge')->getBadgeQueryList($request->input());

        return Response::sendList($list);
    }

}
